package cn.csust.net2.manager.client.ux;

import java.util.List;
import java.util.Map;
import cn.csust.net2.manager.shared.service.ByGradeService;
import cn.csust.net2.manager.shared.service.ByGradeServiceAsync;
import cn.csust.net2.manager.shared.vo.TreeLeaf;
import cn.csust.net2.manager.shared.vo.TreeNode;
import com.extjs.gxt.ui.client.data.BaseTreeLoader;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.data.TreeBeanModelReader;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
@Deprecated
public class BeanTreeFactory {
	private BeanTreeFactory() {

	}

	/**
	 * @param configs
	 * @return
	 */
	private static TreePanel<BeanModel> build(final Map<String, Object> configs) {
		
//		final ByGradeServiceAsync treeService = GWT.create(ByGradeService.class);
		final ByGradeServiceAsync treeService = GWT.create(ByGradeService.class);
		RpcProxy<List<TreeNode>> treeProxy = new RpcProxy<List<TreeNode>>() {

			@Override
			protected void load(Object loadConfig,	AsyncCallback<List<TreeNode>> callback) {
				System.out.println("调用一次11111111111111111");
						if(loadConfig instanceof BeanModel&&loadConfig!=null){
							TreeNode tn = ((BeanModel)loadConfig).getBean();
							treeService.getChildren(tn,null, callback);
						}else{
							treeService.getChildren((TreeNode)loadConfig,null, callback);
						}
				
				
//				treeService.getChildren( (RemoteSortTreeLoadConfig) loadConfig, callback);
//				loadConfig = (Object)loadConfig;
				
				
				if (loadConfig == null)
					System.out.println("在传输前，loadConfig的为NLULL");
//				if (loadConfig != null && loadConfig instanceof BeanModel) {
//					BeanModel bm = (BeanModel) loadConfig;
//					TreeNode tn = bm.getBean();
//					treeService.getChildren(tn, callback);
//				} else {
//					
//					treeService.getChildren((TreeNode) loadConfig, callback);
//				}
//				
				
				
				
				
//				for (Entry<String, Object> en : configs.entrySet()) {
//					String key = en.getKey();
//					Object value = en.getValue();
//					System.out.println(key + "==" + value);
//				}
				//
				// BeanModel bm = (BeanModel) loadConfig;
				// if(bm!=null){
				// TreeNode tn = bm.getBean();
				// System.out.println(tn.getNodeName());
				// }
				// treeService.getChildren((BeanModel)loadConfig, callback);
			}

		};
		TreeBeanModelReader reader = new TreeBeanModelReader();
		
		/**
		 * 
		 * *********************************************************************
		 * ********************* 因为ModelBean是不可以直接被实力化的，所以当返回类型为LIST类型时候，
		 * BeanModelreader会用工厂去实例化每个bean，在其中如果这个属性不设置将会自动的
		 * 将所有的LIST中的BEAN都转化为第一个PO的类型，在这里因为返回的LIST中包含了2中对象一个是专业另外一个是老师，所以
		 * 需要设置该属性，这样每个bean都将得到自己的工厂 *******************
		 * 效率不高，考虑复写解析工厂的算法（GXT）**********************************
		 */
		reader.setFactoryForEachBean(true);
		BaseTreeLoader<BeanModel> loader = new BaseTreeLoader<BeanModel>(treeProxy,reader) {
			@Override
			public boolean hasChildren(BeanModel parent) {
				return !(parent.getBean() instanceof TreeLeaf);
			}
		};
		TreeStore<BeanModel> store = new TreeStore<BeanModel>(loader);
		/**
		 * 装在初始化条件
		 */
		// if(configs!=null){
		//
		// Object lastConfig = loader.getLastConfig();
		// if(lastConfig==null){
		// lastConfig = new BaseRemoteSortTreeLoadConfig();
		// }
		// for(Entry<String, Object> en :configs.entrySet()){
		// String key = en.getKey();
		// Object value = en.getValue();
		// ((ModelData)lastConfig).set(key, value);
		// }
		// loader.load(lastConfig);
		// }
		// else{
		// loader.load();
		// }
		// loader.load();

		TreePanel<BeanModel> tree = new TreePanel<BeanModel>(store);
		
		tree.setStateful(true);
		tree.setDisplayProperty("nodeName");
		// Object lastConfig = loader.getLastConfig();
		// if(configs!=null&&lastConfig==null){
		// lastConfig = Root.initialize(configs);
		// if(lastConfig==null){
		// System.out.println("nullnullnull");
		// }
		// loader.load(lastConfig);
		// // loader.loadChildren((BeanModel) lastConfig);
		//
		// }else{
		// loader.load();
		// }

		return tree;
	}

	public static TreePanel<BeanModel> createComonTree() {

		return build(null);
	}

	/**
	 * 来条件的LOAD
	 * 
	 * @param configs
	 * @return
	 */
	public static TreePanel<BeanModel> createComonTree(
			Map<String, Object> configs) {

		return build(configs);

	}
}
